//
//  DetailCompereViewController2.m
//  JZMCFM
//
//  Created by Mc on 15/11/2.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "DetailCompereViewController2.h"
#import "MusicPlayViewController.h"
#import "DetailCellHelper2.h"
#import "DetailTableViewCell2.h"
@interface DetailCompereViewController2 ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *mainTableView;
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *label1;
@property(nonatomic,strong)UILabel *label2;
@property(nonatomic,assign)BOOL open;
@property(nonatomic,strong)MusicPlayViewController *vc;
@end

@implementation DetailCompereViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    
    [self.mainTableView registerNib:[UINib nibWithNibName:@"DetailTableViewCell2" bundle:nil] forCellReuseIdentifier:@"a"];
    
    [[DetailCellHelper2 sharedInstance] downLoadWithUrlString:[NSString stringWithFormat:@"%@%@%@",kDetailDownFirst2,self.uid,kDetaiDownLast2] withResultBlock:^{
        [self.mainTableView reloadData];
    }];
    NSLog(@"%@",[NSString stringWithFormat:@"%@%@%@",kDetailDownFirst2,self.uid,kDetaiDownLast2]);
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [DetailCellHelper2 sharedInstance].cellArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"a" forIndexPath:indexPath];
    DetailModel2 *p = [DetailCellHelper2 sharedInstance].cellArray[indexPath.row];
    [cell setCellWithModel:p];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 280)];
    view.userInteractionEnabled = YES;
    /*
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//    [manager GET:[NSString stringWithFormat:@"%@%@%@",kDetailFirst2,self.uid,kDetailLast2] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"%@",responseObject);
//        DetailTopModel *model = [[DetailTopModel alloc] init];
//        [model setValuesForKeysWithDictionary:responseObject];
//        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
//        [imageV sd_setImageWithURL:[NSURL URLWithString:model.backgroundLogo]];
//        
//        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(137.5, 20, 100, 100)];
//        [imageView sd_setImageWithURL:[NSURL URLWithString:model.mobileLargeLogo]];
//        [imageV addSubview:imageView];
//        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageView.frame), CGRectGetMaxY(imageView.frame) + 5, CGRectGetWidth(imageView.frame),30)];
//        [imageV addSubview:label1];
//        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(label1.frame), 335, 60)];
//        label2.numberOfLines = 0;
//        [imageV addSubview:label2];
//        UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(imageV.frame) + 10, 105, 60)];
//        label3.text = [NSString stringWithFormat:@"%ld",model.followings];
//        label3.textAlignment = NSTextAlignmentCenter;
//        [view addSubview:label3];
//        UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label3.frame) + 10, CGRectGetMinY(label3.frame), CGRectGetWidth(label3.frame), CGRectGetHeight(label3.frame))];
//        label3.text = [NSString stringWithFormat:@"%ld",model.followers];
//        label3.textAlignment = NSTextAlignmentCenter;
//        [view addSubview:label4];
//        UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label4.frame) + 10, CGRectGetMinY(label3.frame), CGRectGetWidth(label3.frame), CGRectGetHeight(label3.frame))];
//        label5.text = [NSString stringWithFormat:@"%ld",model.favorites];
//        label5.textAlignment = NSTextAlignmentCenter;
//        [view addSubview:label5];
//        UILabel *label6 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(label3.frame), CGRectGetMaxY(label3.frame), CGRectGetWidth(label3.frame), 30)];
//        label6.textAlignment = NSTextAlignmentCenter;
//        label6.text = @"关注";
//        [view addSubview:label6];
//        
//        UILabel *label7 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label6.frame), CGRectGetMaxY(label3.frame), CGRectGetWidth(label3.frame), 30)];
//        label7.textAlignment = NSTextAlignmentCenter;
//        label7.text = @"粉丝";
//        [view addSubview:label7];
//        
//        UILabel *label8 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label7.frame), CGRectGetMaxY(label3.frame), CGRectGetWidth(label3.frame), 30)];
//        label8.textAlignment = NSTextAlignmentCenter;
//        label8.text = @"赞过的";
//        [view addSubview:label8];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"1111111111%@1111111111111",error);
//    }];
    */
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@",kDetailFirst2,self.uid,kDetailLast2]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        DetailTopModel *model = [[DetailTopModel alloc] init];
        [model setValuesForKeysWithDictionary:result];
        
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, 200)];
        [imageV sd_setImageWithURL:[NSURL URLWithString:model.backgroundLogo]];
        [view addSubview:imageV];
        UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        
        UIVisualEffectView *view1 = [[UIVisualEffectView alloc]initWithEffect:beffect];
        
        view1.frame = imageV.bounds;
        [imageV addSubview:view1];
        
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(137.5, 20, 100, 100)];
        self.imageView.layer.cornerRadius = 50;
        self.imageView.clipsToBounds = YES;
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.mobileLargeLogo]];
        [imageV addSubview:self.imageView];
        self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.imageView.frame), CGRectGetMaxY(self.imageView.frame) + 5, CGRectGetWidth(self.imageView.frame),20)];
        self.label1.text = model.nickname;
        self.label1.textAlignment = NSTextAlignmentCenter;
        [imageV addSubview:self.label1];
        
        self.label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.label1.frame), 335, 50)];
        self.label2.textAlignment = NSTextAlignmentCenter;
        self.label2.text = model.personalSignature;
        self.label2.numberOfLines = 0;
        [imageV addSubview:self.label2];
        
        UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(imageV.frame) + 5, 105, 60)];
        [view addSubview:label3];
        
        UILabel *label11 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(label3.frame), CGRectGetHeight(label3.frame) / 2)];
        label11.text = [NSString stringWithFormat:@"%ld",model.followings];
        label11.textAlignment = NSTextAlignmentCenter;
        [label3 addSubview:label11];
        UILabel *label44 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label3.frame), CGRectGetMinY(label3.frame) + 10, 10, 40)];
        label44.alpha = 0.4;
        label44.font = [UIFont systemFontOfSize:23];
        label44.text = @"|";
        label44.textAlignment = NSTextAlignmentCenter;
        [view addSubview:label44];
        UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label3.frame) + 10, CGRectGetMinY(label3.frame), CGRectGetWidth(label3.frame), CGRectGetHeight(label3.frame))];
        [view addSubview:label4];
        
        UILabel *label22 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(label4.frame), CGRectGetHeight(label4.frame) / 2)];
        label22.text = [NSString stringWithFormat:@"%.2f万",model.followers / 10000];
        label22.textAlignment = NSTextAlignmentCenter;
        [label4 addSubview:label22];
        
        UILabel *label55 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label4.frame), CGRectGetMinY(label3.frame) + 10, 10, 40)];
        label55.font = [UIFont systemFontOfSize:23];
        label55.alpha = 0.4;
        label55.text = @"|";
        label55.textAlignment = NSTextAlignmentCenter;
        [view addSubview:label55];
        
        UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label4.frame) + 10, CGRectGetMinY(label3.frame), CGRectGetWidth(label3.frame), CGRectGetHeight(label3.frame))];
        [view addSubview:label5];
        
        UILabel *label33 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(label5.frame), 30)];
        label33.text = [NSString stringWithFormat:@"%ld",model.favorites];
        label33.textAlignment = NSTextAlignmentCenter;
        [label5 addSubview:label33];
        
        UILabel *label6 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(label11.frame), CGRectGetMaxY(label11.frame), CGRectGetWidth(label3.frame), CGRectGetHeight(label3.frame) / 2)];
        label6.textAlignment = NSTextAlignmentCenter;
        label6.alpha = 0.5;
        label6.text = @"关注";
        [label3 addSubview:label6];
        
        UILabel *label7 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(label22.frame), CGRectGetMaxY(label22.frame), CGRectGetWidth(label22.frame), 30)];
        label7.alpha = 0.5;
        label7.textAlignment = NSTextAlignmentCenter;
        label7.text = @"粉丝";
        [label4 addSubview:label7];
        
        UILabel *label8 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(label33.frame), CGRectGetMaxY(label33.frame), CGRectGetWidth(label33.frame), 30)];
        label8.alpha = 0.5;
        label8.textAlignment = NSTextAlignmentCenter;
        label8.text = @"赞过的";
        [label5 addSubview:label8];
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [view addGestureRecognizer:tap];
    return view;
}

- (void)tapAction:(UITapGestureRecognizer *)sender
{
    self.open = !self.open;
    if (self.open) {
        
        [UIView animateWithDuration:1.0 animations:^{
            self.label1.frame = CGRectMake(CGRectGetMinX(self.imageView.frame), 10, CGRectGetWidth(self.imageView.frame),20);
            self.label2.frame = CGRectMake(20, CGRectGetMaxY(self.label1.frame), 335, 100);
            self.imageView.frame = CGRectMake(137.5, 0, 100, 100);
            self.imageView.alpha = 0;
            
            self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_1_PI);
        }];
    }else{
        [UIView animateWithDuration:1.0 animations:^{
            self.label1.frame = CGRectMake(CGRectGetMinX(self.imageView.frame), CGRectGetMaxY(self.imageView.frame) + 5, CGRectGetWidth(self.imageView.frame),20);
            self.label2.frame = CGRectMake(20, CGRectGetMaxY(self.label1.frame), 335, 60);
            self.imageView.frame = CGRectMake(137.5, 20, 100, 100);
            self.imageView.alpha = 1;
            self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_1_PI);
        }];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 265;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MusicPlayViewController *vc = [MusicPlayViewController sharedInstance];
    vc.index = indexPath.row;
    vc.isAgo = NO;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
